#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re

import allure
from playwright.sync_api import Page, expect

from models.base_page import BasePage
from models.network_page.vxnet_page.vxnet_details_page import VxnetDetailPage
from utils.log import log


class VxnetPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.locator_vxnet_name = self.page.locator('input[name="vxnet_name"]')
        self.locator_vxnet = self.page.locator("a").filter(has_text=re.compile(r"^创建$"))
        self.route = "vxnets"
        self.details_page = VxnetDetailPage(page)

    @allure.step("点击创建按钮")
    def create_vxnet_but(self) -> None:
        self.locator_vxnet.click()

    @allure.step("输入私有网络名称")
    def input_vxnet_name(self, vxnet_name: str) -> None:
        self.locator_vxnet_name.click()
        self.locator_vxnet_name.type(vxnet_name, delay=10)

    @allure.step("点击提交按钮")
    def click_create_submit_but(self) -> None:
        self.popout.click_submit_by_locator()

    @allure.step("输入私有网络描述")
    def input_description(self, description: str) -> None:
        self.locator('textarea[name="description"]').type(description, delay=10)

    def assert_create_is_success(self, vxnet_name: str) -> None:
        locator = self.table.get_cell_by_keyword(keyword=vxnet_name)
        expect(locator).to_be_visible()

    @allure.step("table操作-根据关键字操作资源")
    def delte_vxnet_by_name(self, row_keyword: str) -> None:
        self.table.click_checkbox_by_keyword(row_keyword=row_keyword)

    @allure.step("获取删除私有网络弹框警告内容")
    def get_delete_vxnet_alert_info(self):
        """获取删除私有网络弹框警告内容"""
        alert_text_content = self.popout.get_alert_text_content()
        return alert_text_content

    def get_leave_router_alert_info_text_content(self):
        """"""
        if self.page.locator("#modalId").locator(".alert-info").is_visible():
            alert_text_content = (
                self.page.locator("#modalId").locator(".alert-info").text_content()
            )
            log.info(f"弹框文本:{alert_text_content}")
            return alert_text_content

    def check_vxnet_page_user_guide_element_to_be_visible(self):
        """检查使用指南元素是否可见"""
        with allure.step("检查VxnetPage:使用指南元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".intro"))  # 使用指南栏目
            self.expect.to_be_visible(self.page.get_by_text("使用指南"))
            self.expect.to_be_visible(self.page.get_by_text("收起"))
            self.expect.to_be_visible(self.page.locator(".svg-icon > use"))
            self.expect.to_contain_text(self.page.locator(".tab-intro .text"),
                                        "私有网络 (VxNet) 用于云服务器之间互联，它类似物理交换机 (L2Switch) 组成的局域网。"
                                        "不同用户的私有网络之间100%隔离。")

    def check_vxnet_page_table_element_to_be_visible(self):
        """检查工具栏及table是否可见"""
        with allure.step("检查VxnetPage:工具栏及table是否可见"):
            self.expect.to_be_visible(self.page.locator(".table-toolbar"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="G", exact=True))
            self.expect.to_be_visible(self.page.locator("a").filter(has_text=re.compile(r"^创建$")))
            self.expect.to_be_visible(self.page.locator("select[name=\"search_key\"]"))
            self.expect.to_be_visible(self.page.get_by_placeholder(" "))
            self.expect.to_be_visible(self.page.locator("select[name=\"limit\"]"))
